package com.huaxin.device.mapping;

import com.huaxin.device.dto.request.EquipmentDataSourceCreateReqDTO;
import com.huaxin.device.dto.request.EquipmentDataSourceUpdateReqDTO;
import com.huaxin.device.models.EquipmentDataSource;
import com.huaxin.hxenum.enums.EquipmentDataType;
import com.huaxin.hxenum.enums.Unit;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

import java.util.List;

@Mapper(componentModel = "spring")
public interface EquipmentDataSourceMapping {

    @Mapping(target = "dataType", ignore = true)
    @Mapping(target = "unit", ignore = true)
    EquipmentDataSource toEntity(EquipmentDataSourceCreateReqDTO in);

    @AfterMapping
    default void afterMapping(EquipmentDataSourceCreateReqDTO in, @MappingTarget EquipmentDataSource entity) {
        entity.setDataType(EquipmentDataType.of(in.getDataType()));
        if (in.getUnit() != null) {
            entity.setUnit(Unit.of(in.getUnit()));
        }
    }

    @Mapping(target = "dataType", ignore = true)
    @Mapping(target = "unit", ignore = true)
    List<EquipmentDataSource> toEntityList(List<EquipmentDataSourceCreateReqDTO> in);

    @Mapping(target = "dataType", ignore = true)
    @Mapping(target = "unit", ignore = true)
    void updateEntity(EquipmentDataSourceUpdateReqDTO in, @MappingTarget EquipmentDataSource entity);

    @AfterMapping
    default void afterMapping(EquipmentDataSourceUpdateReqDTO in, @MappingTarget EquipmentDataSource entity) {
        entity.setDataType(EquipmentDataType.of(in.getDataType()));
        if (in.getUnit() != null) {
            entity.setUnit(Unit.of(in.getUnit()));
        }
    }

    @Mapping(target = "dataType", ignore = true)
    @Mapping(target = "unit", ignore = true)
    void updateEntityList(List<EquipmentDataSourceUpdateReqDTO> in, @MappingTarget List<EquipmentDataSource> entity);
}